package cn.ittaoli.szdz.controller;

import cn.ittaoli.szdz.model.CxMember;
import cn.ittaoli.szdz.service.IndexService;
import com.google.code.kaptcha.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by lub_lenovo on 2017/11/8.
 */
@Controller
@RequestMapping("/index")
public class IndexController {
    @Autowired
    private IndexService indexService;


    @RequestMapping("/login")
    public String login(){
        return "index/login";
    }

    @RequestMapping("/doLogin")
    public String doLogin(CxMember cxMember, String verify, HttpServletRequest request,RedirectAttributes attr){
        String codeCorrect=(String)request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);

        String result=indexService.login(cxMember,verify,codeCorrect,request);
        String msg="";
        String url="";
        switch (result){
            case "0":
                msg="用户名或密码错误";
                url= "redirect:/index/login";
                break;
            case "1":
                msg="登陆成功";
                url= "redirect:/index/main";
                break;
            case "-1":
                msg="验证码错误";
                url= "redirect:/index/login";
                break;
        }
        attr.addAttribute("msg",msg);
        return url;
    }
    @RequestMapping("/main")
    public String main(){
        return "index/main";
    }
}
